package com.tao.user.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tao.constant.MessageConstant;
import com.tao.constant.StatusConstant;
import com.tao.user.domain.dto.LoginDto;
import com.tao.user.domain.dto.PageSelectUserDto;
import com.tao.user.domain.dto.UserDto;
import com.tao.user.domain.pojo.User;
import com.tao.user.domain.vo.LoginVo;
import com.tao.user.domain.vo.UserVo;
import com.tao.exception.AccoundNotFoundExceotion;
import com.tao.exception.AccountLockedEcxeption;
import com.tao.exception.PasswordErrorException;
import com.tao.user.mapper.UserMapper;

import com.tao.result.PageResult;
import com.tao.user.service.UserService;
import com.tao.utils.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserService1 implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    JWTUtil jwtUtil;

    @Override
    public LoginVo loginService(LoginDto loginDto) {
        User user = userMapper.loginMapper(loginDto);

        if (user == null) throw new AccoundNotFoundExceotion(MessageConstant.ACCOUNT_NOT_FOUND);

        if (!(user.getPassword().equals(loginDto.getPassword()))) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (user.getStatus() == StatusConstant.DISABLE)
            throw new AccountLockedEcxeption(MessageConstant.ACCOUNT_LOCKED);
        Map<String, Object> m = new HashMap<>();
        m.put("id", user.getId());
        m.put("name", user.getName());
        m.put("roleId",user.getRoleId());
        String jwt = jwtUtil.createJWT(m);
        log.info("JWT令牌为：{}", jwt);
        LoginVo loginVo = LoginVo.builder()
                .id(user.getId())
                .name(user.getName())
                .token(jwt)
                .build();
        log.info("loginVo:{}", loginVo.getName());
        return loginVo;
    }

    /**
     * @param userDto
     * @return
     */
    @Override
    public void userAddService(UserDto userDto) {
        log.info("Service层接收到Controller层数据，用户{}", userDto.getName());
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        log.info("包装后的User:{}", user);
        userMapper.userAddMapper(user);
        log.info("成功添加用户:{}", user.getName());

    }

    @Override
    public PageResult pageSelectService(PageSelectUserDto pageSelectUserDto) {
        log.info("接收到的参数为:{}",pageSelectUserDto);
        PageHelper.startPage(pageSelectUserDto.getPage(), pageSelectUserDto.getPageSize());

        Page<UserVo> page = userMapper.pageSelectUserMapper(pageSelectUserDto);

        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        log.info("结果为:{}",pageResult);
        return pageResult;


    }

    @Override
    public void updateUserService(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        userMapper.updateUserMapper(user);
    }

    @Override
    public User getUserByIdService(Integer id) {
        log.info("根据id查询用户Service层接收到参数：{}", id);
        return userMapper.getUserByIdMapper(id);
    }

    @Override
    public void updateStatus(Integer status, Integer id) {
        log.info("Service接收到参数Status:{},id:{}", status, id);
        if (status.equals(StatusConstant.DISABLE))
            userMapper.updateStatus(StatusConstant.DISABLE, id);
        else
            userMapper.updateStatus(StatusConstant.ENABLE, id);
        log.info("状态修改完成");
        log.info("修改后用户信息：{}", userMapper.getUserByIdMapper(id));
    }

    @Override
    public void deleteUser(List<Integer> ids) {

        try {
            userMapper.deleteUser(ids);
        } catch (Exception e) {
            throw  new RuntimeException("有关联表信息，无法删除");
        }
        log.info("成功删除用户：{}",ids);
    }
}
